<template>
  <div>
    <div class="options">
      <el-scrollbar ref="viewScrollbar" @scroll="handleScroll"
                    :style="{height:(maxHeight -160) + 'px'}">
        <el-checkbox-group class="w-1/1" v-model="viewIds">
          <el-row :gutter="24" justify="space-between">
            <el-col :xl="6" :lg="6" :md="24" :sm="24" :xs="24"
              v-for="view in page.list"
              :key="view.id">
              <el-card body-class="" class="image-card">
                <div>
                  <div style="padding: 5px;" v-if="view.content">
                    <el-image v-if="!view.vi" style="border-radius: 5px;"
                              draggable="false"
                              @click.prevent="handleView(view)"
                              class="el-img"
                              :src="view.icon"
                              :title="'点击查看3D'"
                              fit="cover"
                    />
                    <div v-else class="container" style="height: 256px; border-radius: 5px;">
                      <div v-html="view.content"></div>
                    </div>
                  </div>
                  <div v-else style="padding: 5px;">
                    <el-image style="border-radius: 5px;"
                              draggable="false"
                              class="el-img"
                              :src="view.icon"
                              :title="view.name"
                              fit="cover"
                    />
                  </div>
                </div>
                <div style="display: flex;justify-content: center;">
                  <div style="text-align: center; margin-left: 10px;">
                    <el-checkbox :key="view.id" :value="view.id" :label="view.name"/>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-checkbox-group>
      </el-scrollbar>
    </div>
    <div style="display: flex; justify-content: center; padding: 5px;">
      <el-button type="primary" round @click="handlerOk">确认</el-button>
      <el-button round @click="handlerCancel">取消</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ViewApi } from "@/api/threed";
const emits = defineEmits(['handlerOk', 'handlerCancel'])
const viewIds = ref([])
/** 预览按钮操作 */
const handleView = (view) => {
  page.value.list.forEach(v => {
    v.vi = false;
  })
  view.vi = true
}

const handlerOk = () => {debugger
  emits('handlerOk', viewIds)
}

const handlerCancel = () => {
  emits('handlerCancel')
}

/** 查询列表 */
const page = ref({
  pageNo: 1,
  pageSize: 10,
  list: [],
  total: 0,
})

const viewScrollbar = ref(null);
const handleScroll = () => {
  if (viewScrollbar.value) {
    const wrap = viewScrollbar.value.wrapRef;
    const tolerance = 10; // 容差值，可以根据需要调整
    // 检查是否滚动到底部
    //console.log(wrap.clientHeight + wrap.scrollTop, wrap.scrollHeight)
    if (wrap.clientHeight + wrap.scrollTop >= wrap.scrollHeight - tolerance) {
      console.log('到达底部');
      // 在这里处理到达底部的逻辑
      page.value.pageNo += 1;
      //console.log(Math.ceil(page.value.total / page.value.pageSize))
      if(page.value.pageNo > Math.ceil(page.value.total / page.value.pageSize)){
        return;
      }
      //console.log(page.value.pageNo, page.value.pageSize, page.value.total)
      getList({
        'pageNo': page.value.pageNo,
        'pageSize': page.value.pageSize,
      })
    }
  }
};
const getList = async (pageParam) => {
  try {
    //无分页参数，则查询第一页
    if(!pageParam || !pageParam.pageNo || !pageParam.pageSize){
      page.value.list = []
    }
    // 执行查询
    const response = await ViewApi.getViewPage({
      'pageNo': pageParam?pageParam.pageNo: 1,
      'pageSize': pageParam?pageParam.pageSize: 20,
    });
    const list = response.list;
    const total = response.total;
    if(list && list.length){
      page.value.list.push(...list);
      page.value.total = total;
    }

  } catch (error) {
    console.error('查询模型数据失败:', error);
    return { list: [], total: 0 };
  }
}
//高度
const maxHeight = ref(600)
const updateMaxHeight = () => {
  maxHeight.value = document.body.scrollHeight;
};
onMounted(() => {
  if (viewScrollbar.value) {
    viewScrollbar.value.wrapRef.addEventListener('scroll', handleScroll);
  }
  getList({})
  updateMaxHeight();
  window.addEventListener('resize', updateMaxHeight);
});
onBeforeUnmount(() => {
  if (viewScrollbar.value) {
    viewScrollbar.value.wrapRef.removeEventListener('scroll', handleScroll);
  }
  window.removeEventListener('resize', updateMaxHeight);
});

</script>
<style  lang="scss">
.container {
  width: 100%;
  height: 100vh; /* 或者你想要的高度 */
  border: 1px solid #000; /* 可选，用于查看边界 */
  overflow: hidden; /* 确保内容不会溢出 */
  position: relative; /* 使iframe的绝对定位相对于这个div */
}
.container iframe {
  width: 100%;
  height: 100%;
  border: none; /* 去掉iframe的边框 */
  position: absolute; /* 使iframe绝对定位 */
  top: 0;
  left: 0;
}
.image-card {
  margin: 10px;
  height: auto;
  border-radius: 10px;
  position: relative;
  display: flex;
  flex-direction: column;
}
</style>
